package com.alinesno.cloud.busines.shop.manage.rest;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.date.DateUtil;
import com.alinesno.cloud.busines.shop.manage.entity.ItemEntity;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean; 
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;

import com.alinesno.cloud.busines.shop.manage.entity.ItemKillEntity;
import com.alinesno.cloud.busines.shop.manage.service.IItemKillService;

import java.util.Date;
import java.util.List;

/**
 * 待秒杀商品Rest
 *
 * @author wangyongkang ${authorEmail}
 * @date 2022-03-17 23:48:40
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("/api/shop-manage/ItemKill")
public class ItemKillRest extends BaseRestAdapter<ItemKillEntity, IItemKillService> {

    //日志记录
    private static final Logger log = LoggerFactory.getLogger(ItemKillRest.class);

    @Autowired
    private IItemKillService itemKillService;

    @TranslateCode
    @ResponseBody
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
        log.debug("page = {}", ToStringBuilder.reflectionToString(page));
        return this.toPage(model, this.getFeign() , page) ;
    }

    @Override
    public IItemKillService getFeign() {
        return this.itemKillService;
    }

    @ResponseBody
    @PostMapping("/newSave")
    public AjaxResult newSave(@RequestBody List<ItemEntity> entities){

        for(ItemEntity entity:entities){

            ItemKillEntity killEntity=new ItemKillEntity();
            killEntity.setItemId(entity.getId());
            killEntity.setIsActive(Byte.parseByte("0"));
            killEntity.setCreateTime(DateUtil.date());
            killEntity.setStartTime(null);
            killEntity.setEndTime(null);
            killEntity.setTotal(0);
            itemKillService.save(killEntity);
        }
        return AjaxResult.success("保存成功！");
    }

}
